<template>
    <el-card class="box-card">
        <!-- 卡片顶部添加按钮 -->
        <el-button type="primary" size="default" icon="Plus" @click="controlForm">Primary</el-button>
        <!-- 表格组件 -->
        <el-table :data="trademarkArr" style="margin: 10px 0px;">
            <el-table-column label="序号" width="80px" align="center" type="index"></el-table-column>
            <!-- table-column:默认展示数据用div 如果想用其他标签可以用插槽-->
            <el-table-column label="品牌名称" prop="tmName">
            </el-table-column>
            <el-table-column label="品牌LOGO">
                <template #="{ row, column, $index }">
                    <!-- <h1>{{ column }}</h1> 这里的插槽有三个参数分别是 row，数据对象；column行的信息；$index他的序号-->
                    <img :src="row.logoUrl" style="width:100px;height: 100px;">
                </template>
            </el-table-column>
            <el-table-column label="品牌操作">
                <template #="{ row, $index }">
                    <el-button type="primary" icon="Edit" @click="updateTrademark(row)">更新</el-button>
                    <el-popconfirm :title="`您确定要删除${row.tmName}?`" width="250px" icon="Delete"
                        @confirm='removeTradeMark(row.id)'>
                        <template #reference>
                            <el-button type="primary" icon="Delete">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="sizeChange" @current-change="getHasTrademark" :pager-count="9"
            v-model:current-page="pageNo" v-model:page-size="limit" :page-sizes="[3, 5, 7, 9]" :background="true"
            layout="prev, pager, next, jumper,->,sizes,total" :total="total" />

        <!-- 出来的弹框 -->
        <el-dialog v-model="dialogFormVisible" title="添加品牌">
            <el-form :model="form">
                <el-form-item label="品牌名称" :label-width="formLabelWidth">
                    <el-input v-model="form.tmName" autocomplete="off" />
                </el-form-item>
                <el-form-item label="品牌LOGO" :label-width="formLabelWidth">
                    <!-- upload组件，action图片上传路径书写/api，代理服务器不发送这次post请求 -->
                    <el-upload class="avatar-uploader" action="/api/admin/product/fileUpload" :show-file-list="false"
                        :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img class="avatar" />
                        <el-icon class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancelTank">Cancel</el-button>
                    <el-button type="primary" @click="confirmTank">
                        Confirm
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>
<script setup lang="ts">
// 引入组合式api函数
import { ref, onMounted, reactive, nextTick } from 'vue';
import { reqHasTrademark, reqAddOrUpdateTrademark, reqDeleteTrademark } from "@/api/product/trademark/index"
import type { Records, TradeMarkResponseData, TradeMark } from '@/api/product/trademark/type'
import { ElMessage } from 'element-plus';
import { UploadProps } from 'element-plus/lib/components/upload/src/upload.js';
//分页请求和操作
//当前页码
let pageNo = ref<number>(1);
//每一页展示多少条数据
let limit = ref<number>(3);
//存储已有品牌数据总数
let total = ref<number>(0);
//存储已有品牌的数据
let trademarkArr = ref<Records>([]);
//获取el-form组件实例
// let formRef = ref();
//获取已有品牌的接口封装为一个函数:在任何情况下向获取数据,调用次函数即可
const getHasTrademark = async (pager = 1) => {
    //当前页码
    pageNo.value = pager;
    let result: TradeMarkResponseData = await reqHasTrademark(pageNo.value, limit.value);
    if (result.code == 200) {
        //存储已有品牌总个数
        total.value = result.data.total;
        trademarkArr.value = result.data.records;
    }
}
//组件挂载完毕钩子---发一次请求,获取第一页、一页三个已有品牌数据
onMounted(() => {
    getHasTrademark();
});

//当下拉菜单发生变化的时候触发次方法
//这个自定义事件,分页器组件会将下拉菜单选中数据返回
const sizeChange = () => {
    //当前每一页的数据量发生变化的时候，当前页码归1
    getHasTrademark();
}

//控制对话框显示与隐藏
let dialogFormVisible = ref<boolean>(false)
const controlForm = () => {
    dialogFormVisible.value = true
}
const formLabelWidth = '140px'
const form = reactive<TradeMark>({
    tmName: '',
    logoUrl: ''
})
//定义收集新增品牌数据
let trademarkParams = reactive<TradeMark>({
    tmName: '',
    logoUrl: ''
})

// 定义更新品牌方法
let updateTrademark = (row: any) => {
    dialogFormVisible.value = true
    console.log(row)
    form.tmName = row
}


// 上传图片组件->上传图片之前触发的钩子函数
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    // 钩子是在图片上传成功之前触发，上传文件之前可以约束文件类型与大小
    // 要求：上传文件格式png|jpg|gif 4M
    if (rawFile.type == 'image/png' || rawFile.type == 'image/jpeg' || rawFile.type == 'image/gif') {

    } else {
        ElMessage({
            type: 'error',
            message: "上传文件格式务必PNG|JPG|GIF"
        })
        return false;
    }
}


// 确认按钮
let confirmTank = () => {
    dialogFormVisible.value = false
    // const params = form
    // reqAddOrUpdateTrademark(params: any).then()
}

// 取消按钮
let cancelTank = () => {
    dialogFormVisible.value = false
    // const params = form
    // reqAddOrUpdateTrademark(params: any).then()
}
</script>
<style scoped lang="scss"></style>


